<?php
//require 访问控制类
require_once 'AccessControl.php';
require_once 'Validator.php';

class QuestionController extends Zend_Controller_Action
{
    
    public function init()
    {
        $this->access = new AccessControl(); //实例化访问控制类
        $this->Validator = new Validator(); //实例化验证类
    }

    public function indexAction()
    {
        // action body
    }

    //查看题目
    public function viewAction()
    {
        $qid = $this->getRequest()->getUserParam('qid');
        //判断 aid 是否为数字先
            if(is_numeric($qid))
                $question = $this->Validator->isQidValid($qid);
            else
                $question = false;
        
        //判断是否传入公告ID和相应题目是否存在
        if ($question == FALSE)
        {
            $this->view->access = false;//不允许访问
            $this->view->ErrorInfo = Validator::QidError;
            return;
        }
        
        $this->view->question = $question;
        $this->view->admin = $this->access->isAdmin(); //是否管理员
        $this->view->access = true;
    }

    //题目列表
    public function listAction()
    {
        $pid = $this->getRequest()->getUserParam('pid');
        $this->view->showpage = true;
        
        $questionDb = new Application_Model_DbTable_Question();
        if($this->access->isAdmin())
            $ques_count = $questionDb->countQuestionsToAdmin();
        else
            $ques_count = $questionDb->countQuestions();//question 总条数
        
        //是否分页
        if($ques_count <= Validator::QuesListPagesize)
            $this->view->showpage = false;
        else
        {
            if($ques_count % Validator::QuesListPagesize)
                $pages = (INT)($ques_count/Validator::QuesListPagesize+1);//有余数的情况
            else
                $pages = $ques_count/Validator::QuesListPagesize;
            
            $this->view->pages = $pages;
        }
        
        //pid 的判断
        if(!isset($pid))
        {
            $questions = $questionDb->getQuestionsByRange(0,Validator::QuesListPagesize);
            $pid = 1;
        }
        elseif($pid > 1 && $pid <= $pages)
            $questions = $questionDb->getQuestionsByRange(Validator::QuesListPagesize*($pid-1),Validator::QuesListPagesize);
        else
            $this->_redirect('Question/list');//pid 不合法直接跳转首页
        
        $this->view->questions = $questions;
        $this->view->pid = $pid;
        $this->view->login = $this->access->isLogin();
        $this->view->admin = $this->access->isAdmin(); //是否管理员
        
        if(!$this->access->isLogin())
        {
            return;
        }
        
        $answerDb = new Application_Model_DbTable_Answer();
        $userAnswers = $answerDb->getAnswersByUser($this->access->getSession());
        
        //对已提交通过的题目标记,效率很低
        foreach ($userAnswers as $current)
        {
            if($current->getMemo() == '$10000001')
            {
                foreach($questions as $curquestion)
                {
                    if($curquestion->getId() == $current->getQuestion()->getId())
                        $curquestion->setMemo('√');
                }
            }
        }
        
        //Todo:每一题通过比列
        $this->view->question = $questions;
    }

    //添加题目
    public function addAction()
    {
        //检查权限
        if(!$this->access->isLogin())
        {
            $this->view->ErrorInfo = Validator::LoginPlz;
            return;
        }
        elseif(!$this->access->isAdmin())
            $this->view->UserTips = Validator::UserTips;
        
        $questionAddForm = new Application_Form_QuestionAdd();
        $this->view->questionAddForm = $questionAddForm;
        
        $request = $this->getRequest();
        
        if($request->isPost())
        {
            $post = $request->getPost();
            if($questionAddForm->isValid($post))
            {
                $question = new Application_Model_Question();
                
                $mysqldate = date( 'Y-m-d H:i:s',  time());
                
                $question->setChName($post['qchname']);
                $question->setEnName($post['qenname']);
                $question->setScore($post['qscore']);
                $question->setInput($post['qinput']);
                $question->setOutput($post['qoutput']);
                $question->setRequireTime($post['requiretime']);
                $question->setRequireMemory($post['requiremem']);
                $question->setChContent($post['chcontent']);
                $question->setEnContent($post['encontent']);
                $question->setTime($mysqldate);
                $question->setUser($this->access->getSession());
                
                if(!$this->access->isAdmin())
                {
                    $question->setMemo('wait');
                }
                                
                $questionDb = new Application_Model_DbTable_Question();
                //写入数据库
                $questionDb->addQuestion($question);
                $this->_redirect('/Question/list');
            }
        }
        
    }

    //修改题目
    public function updateAction()
    {
        //是否管理员
        if(!$this->access->isAdmin())
        {
            $this->view->ErrorInfo = Validator::PermissionDenied;
            return;
        }
        
        $qid = $this->getRequest()->getUserParam('qid');
        //判断 qid 是否为数字先
            if(is_numeric($qid))
                $question = $this->Validator->isQidValid($qid);
            else
                $question = false;
        
        //判断是否传入公告ID和相应题目是否存在
        if ($question == false)
        {
            $this->view->ErrorInfo = Validator::QidError;
            return;
        }
            
        
        $qUpdateForm = new Application_Form_QuestionAdd();
        $qUpdateForm->qenname->setValue($question->getEnName());
        $qUpdateForm->qchname->setValue($question->getChName());
        $qUpdateForm->qscore->setValue($question->getScore());
        $qUpdateForm->qinput->setValue($question->getInput());
        $qUpdateForm->qoutput->setValue($question->getOutput());
        $qUpdateForm->requiretime->setValue($question->getRequireTime());
        $qUpdateForm->requiremem->setValue($question->getRequireMemory());
        $qUpdateForm->encontent->setValue($question->getEnContent());
        $qUpdateForm->chcontent->setValue($question->getChContent());
        
        $this->view->qUpdateForm = $qUpdateForm;
        
        if ($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            
            $question->setEnName($post['qenname']);
            $question->setChName($post['qchname']);
            $question->setScore($post['qscore']);
            $question->setInput($post['qinput']);
            $question->setOutput($post['qoutput']);
            $question->setRequireTime($post['requiretime']);
            $question->setRequireMemory($post['requiremem']);
            $question->setEnContent($post['encontent']);
            $question->setChContent($post['chcontent']);
            
            
            $questionDb = new Application_Model_DbTable_Question();
            $questionDb->updateQuestion($question);
            
            $this->_redirect('Question/view/qid/'.$qid);
        }
        
    }

    //接收一个用户提交的题目
    public function acceptAction()
    {
        $qid = $this->getRequest()->getUserParam('qid');
        
        //判断 qid 是否为数字先
        if(!is_numeric($qid) || !$question = $this->Validator->isQidValid($qid))
        {
            $this->view->ErrorInfo = Validator::QidError;
            return;
        }
        
        $questionDb = new Application_Model_DbTable_Question();
        $question = $questionDb->getQuestionById($qid);
        $question->setMemo(null);
        $questionDb->updateQuestion($question);
        
        $this->_redirect('Question/list');
    }
    
    //删除题目
    public function deleteAction()
    {
        if (!$this->access->isAdmin())
        {
            $this->view->ErrorInfo = Validator::PermissionDenied;
            return;
        }
        
        $qid = $this->getRequest()->getUserParam('qid');
        
        if(!is_numeric($qid) || $this->Validator->isQidValid($qid) == false)
        {
            $this->view->ErrorInfo = Validator::QidError;
            return;
        }
        
        $questionDb = new  Application_Model_DbTable_Question();
        //从数据库中删除题目
        $questionDb->deleteQuestion($qid);
        $this->_redirect('Question/list');
    }

}
